﻿using System.Collections.Generic;
using System.Threading;
using System.Web;
using MAF.Common;

namespace Web
{
    public class BasePage : MAF.Common.BasePage
    {
        /// <summary>
        /// 是否有权限
        /// </summary>
        public override bool HasPermit(string role)
        {
            return Thread.CurrentPrincipal.IsInRole(role);
        }

        /// <summary>
        /// 本应用的根URL
        /// </summary>
        public override string RootUrl
        {
            get{   return WebConf.AppRootPath;}
        }

        #region QueryString参数
        /// <summary>
        /// 页面版本
        /// </summary>
        public string Version
        {
            get { return GetQuery("Version", WebConf.DefalutVersion); }
        }

        /// <summary>
        /// 模块名称，模块有多个处理程序类组成
        /// </summary>
        public string Module
        {
            get { return GetQuery("Module",WebConf.DefaultModule); }
        }

        /// <summary>
        /// 处理程序类名称，同一个处理程序可以服务于多个模块
        /// </summary>
        public string Handler
        {
            get { return GetQuery("Handler",WebConf.DefalutHandler); }
        }

        /// <summary>
        /// 处理程序执行动作名称，即是处理程序类的方法
        /// </summary>
        public string Action
        {
            get { return GetQuery("Action",WebConf.DefaultAction); }
        }

        #endregion

        /// <summary>
        /// 转换模块名与模块编号
        /// </summary>
        /// <param name="module"></param>
        /// <returns></returns>
        public static string ModuleTranslate(string module)
        {
            if(string.IsNullOrEmpty(module))
                return string.Empty;

            switch (module.ToLower())
            {
                case "triliph": return "产品中心";
                case "news": return "新闻资讯";
                case "knowledge" : return "知识中心";
                case "dealer": return "经销商查询";
                case "message": return "雁过留声";
                case "company": return "公司概况";
                case "nuenjane": return "产品中心";
                case "nnjknowledge" : return "营养与美丽";
                default: return string.Empty;
            }
        }

        /// <summary>
        /// 可被Server.Execute执行的文件路径
        /// </summary>
        public string ExeFilePath
        {
            get
            {
                List<string> list = new List<string>();
                
                list.Add(WebConf.PageRootPath);
                
                if (bool.Parse(WebConf.IsAllowSwitchVersion))
                    list.Add(this.Version);
                else
                    list.Add(WebConf.DefalutVersion);

                if( !string.IsNullOrEmpty(this.Module) )
                    list.Add(this.Module);

                list.Add(this.Action);

                return string.Join("/", list.ToArray()) + ".aspx";
            }
        }
        
        #region 私有方法

        /// <summary>
        /// 读取查询字符串，并设置默认值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="defaultVal"></param>
        /// <returns></returns>
        private static string GetQuery(string key, string defaultVal)
        {
            // 页面版本
            string s = HttpContext.Current.Request.QueryString[key];
            return (null == s) ? defaultVal : s;
        }

        #endregion
    }
}
